#!/usr/bin/perl -w # useful for syntax highlighting

package clreqd_modules;

use Fcntl;
# devname, offset, length

sub diskhb_rawwrite {
my %ARGS=@_;

	#################################################################
	# Rather than calling the binary directly to get the raw 	#
	# block size we do it via the diskhb_blksize routine.		#
	#################################################################
	
	my $_res=diskhb_blksize(DEVNAME => $ARGS{DEVNAME});
	my @F=split(/\s+/,$_res);
	if($F[0] ne "OK") {
		return $_res;
	}
	my $blk_size=int($F[1]);
	my $offset=int($ARGS{OFFSET});
	my $length=int($ARGS{LENGTH});
	my $data=pack("H*",$ARGS{DATA});
	my $padding="\0" x $length;
	$data=substr($data . $padding,0,$length);

	if($offset % $blk_size != 0) {
		return "ERROR Offset is not a multiple of device block size.";
	}
	if($length % $blk_size != 0) {
		return "ERROR Length is not a multiple of device block size.";
	}
	my $fd;
	sysopen($fd,$ARGS{DEVNAME},O_RDWR);
	if(! defined($fd)) {
		return "ERROR Unable to open device '$ARGS{DEVNAME}': $!";
	}
	if(sysseek($fd,$offset,0) != $offset) {
		return "ERROR Unable to seek to offset '$offset' on '$ARGS{DEVNAME}'.";
	}
	if(syswrite($fd,$data,$length) != $length) {
		return "ERROR Unable to write '$length' bytes of data to '$ARGS{DEVNAME}'.";
	}
	close($fd);
	return "OK";
}
1;
